#购物车模拟
class ShoppingCart:
    def __init__(self):
        self.account_balance = float(input("请输入您的账户资金金额："))
        self.goods_dict = {
            1: {"name": "手机", "price": 2000},
            2: {"name": "电脑", "price": 5000},
            3: {"name": "平板", "price": 1500},
            4: {"name": "耳机", "price": 200}
        }
        self.shopping_cart = []

    def show_goods(self):
        print("商品列表：")
        print("{:<10} {:<15} {:<10}".format("商品编号", "商品名称", "单价"))
        for key, value in self.goods_dict.items():
            print("{:<10} {:<15} {:<10}".format(key, value["name"], value["price"]))

    def add_to_cart(self, choice):
        if choice in self.goods_dict:
            selected_goods = self.goods_dict[choice]
            if self.account_balance >= selected_goods["price"]:
                self.shopping_cart.append(selected_goods)
                self.account_balance -= selected_goods["price"]
            else:
                print("账户余额不足！")
        else:
            print("请输入有效的商品编号！")

    def show_cart(self):
        print("您的购物车：")
        print("{:<15} {:<10}".format("商品名称", "单价"))
        for goods in self.shopping_cart:
            print("{:<15} {:<10}".format(goods["name"], goods["price"]))

    def show_balance(self):
        print("账户余额：", self.account_balance)


if __name__ == '__main__':
    cart = ShoppingCart()
    cart.show_goods()

    while True:
        choice = input("请选择要购买的商品编号（输入q退出并结算）：")
        if choice == 'q':
            break
        try:
            choice = int(choice)
            cart.add_to_cart(choice)
        except ValueError:
            print("请输入有效的商品编号！")

    cart.show_cart()
    cart.show_balance()
